package net.pierrerenaudin.blobbyvolley.client;

import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Event.NativePreviewEvent;
import com.google.gwt.user.client.Event.NativePreviewHandler;

public class KeyboardManager {

	private boolean qwertyKeyboard = false;

	public static int UP = 0;
	public static int RIGHT = 1;
	public static int LEFT = 2;

	boolean[] leftPlayerDirections = {false,false,false};
	boolean[] rightPlayerDirections = {false,false,false};

	// Key Code
	final int KEY_A = 65;
	final int KEY_S = 83;
	final int KEY_D = 68;
	final int KEY_Z = 90;
	final int KEY_Q = 81;
	final int KEY_W = 87;
	final int KEY_UP = 38;
	final int KEY_DOWN = 40;
	final int KEY_LEFT = 37;
	final int KEY_RIGHT = 39;


	public KeyboardManager(){

		leftPlayerDirections[UP] = false;
		leftPlayerDirections[LEFT] = false;
		leftPlayerDirections[RIGHT] = false;

		rightPlayerDirections[UP] = false;
		rightPlayerDirections[LEFT] = false;
		rightPlayerDirections[RIGHT] = false;

		Event.addNativePreviewHandler(new NativePreviewHandler() {		
			@Override
			public void onPreviewNativeEvent(NativePreviewEvent event) {
				if(event.getTypeInt() == Event.ONKEYDOWN){
					if(!qwertyKeyboard){
						switch(event.getNativeEvent().getKeyCode()){
						case  KEY_Z:
							leftPlayerDirections[UP] =  true;
							break;
						case  KEY_Q:
							leftPlayerDirections[LEFT] =  true;
							break;
						case  KEY_D:
							leftPlayerDirections[RIGHT] =  true;
							break;
						case  KEY_UP:
							rightPlayerDirections[UP] = true;
							break;
						case  KEY_LEFT:
							rightPlayerDirections[LEFT] = true;
							break;
						case  KEY_RIGHT:
							rightPlayerDirections[RIGHT] = true;
							break;
						}
					}
					else{
						switch(event.getNativeEvent().getKeyCode()){
						case  KEY_W:
							leftPlayerDirections[UP] =  true;
							break;
						case  KEY_A:
							leftPlayerDirections[LEFT] =  true;
							break;
						case  KEY_D:
							leftPlayerDirections[RIGHT] =  true;
							break;
						case  KEY_UP:
							rightPlayerDirections[UP] = true;
							break;
						case  KEY_LEFT:
							rightPlayerDirections[LEFT] = true;
							break;
						case  KEY_RIGHT:
							rightPlayerDirections[RIGHT] = true;
							break;
						}
					}
				}
				if(event.getTypeInt() == Event.ONKEYUP){
					if(!qwertyKeyboard){
						switch(event.getNativeEvent().getKeyCode()){
						case  KEY_Z:
							leftPlayerDirections[UP] = false;
							break;
						case  KEY_Q:
							leftPlayerDirections[LEFT] = false;
							break;
						case  KEY_D:
							leftPlayerDirections[RIGHT] = false;
							break;
						case  KEY_UP:
							rightPlayerDirections[UP] = false;
							break;
						case  KEY_LEFT:
							rightPlayerDirections[LEFT] = false;
							break;
						case  KEY_RIGHT:
							rightPlayerDirections[RIGHT] = false;
							break;
						}
					}
					else{
						switch(event.getNativeEvent().getKeyCode()){
						case  KEY_W:
							leftPlayerDirections[UP] =  false;
							break;
						case  KEY_A:
							leftPlayerDirections[LEFT] =  false;
							break;
						case  KEY_D:
							leftPlayerDirections[RIGHT] =  false;
							break;
						case  KEY_UP:
							rightPlayerDirections[UP] = false;
							break;
						case  KEY_LEFT:
							rightPlayerDirections[LEFT] = false;
							break;
						case  KEY_RIGHT:
							rightPlayerDirections[RIGHT] = false;
							break;
						}
					}
				}
				event.getNativeEvent().preventDefault();
			}
		});
	}

	public boolean isKeyPressForPlayer(Player player,int directionCode){
		if(player.side == Player.RIGHT_PLAYER){
			return rightPlayerDirections[directionCode];
		}
		else if(player.side == Player.LEFT_PLAYER){
			return leftPlayerDirections[directionCode];
		}
		return false;
	}

	public void setQwertyKeyboard(boolean qwertyKeyboard) {
		this.qwertyKeyboard = qwertyKeyboard;
	}

	public boolean isQwertyKeyboard() {
		return qwertyKeyboard;
	}

}

